package org.jeecg.modules.business.third.strategy.emsdms;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTaskMaterial;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskMaterialService;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.business.third.service.IThirdCommonService;
import org.jeecg.modules.dock.entity.mds.MdsBaseResponseVO;
import org.jeecg.modules.dock.entity.mds.request.MDSDeliveryRequestVO;
import org.jeecg.modules.dock.entity.mds.request.MDSTransverseShearingIsCalledMaterialRequestVO;
import org.jeecg.modules.dock.entity.mes.request.CrossShearCallMaterialRequestVO;
import org.jeecg.modules.dock.enums.MdsStatusEnum;
import org.jeecg.modules.dock.service.MdsDockingRepeaterService;
import org.jeecg.modules.flow.sys.constant.FlowConstant;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    横剪叫料(条料) 13 dms
 * 创建人员:    zhaojun
 * 创建时间:    2023-10-23
 */
@Service(value = "c13DmsOutbound")
@Slf4j
@RequiredArgsConstructor
public class C13DmsOutbound implements ThirdOperateStrategy {

    private final IWmsOutReceiptTaskMaterialService wmsOutReceiptTaskMaterialService;
    private final IWmsOutReceiptTaskService wmsOutReceiptTaskService;
    private final MdsDockingRepeaterService mdsDockingRepeaterService;
    private final IThirdCommonService thirdCommonService;

    @Override
    public boolean operate(Map<String, Object> map) {
        AtomicBoolean result = new AtomicBoolean(true);
        Integer type = (Integer) map.get("type");

        WmsOutReceiptTask wmsOutReceiptTask = wmsOutReceiptTaskService.getById(map.get("taskId").toString());
        Optional<WmsOutReceiptTaskMaterial> taskMaterialOptional = thirdCommonService.getWmsOutReceiptTaskMaterials(map.get("taskId").toString());

        try {
            if (ObjectUtils.compare(type, 0) == 0) {
                if (taskMaterialOptional.isPresent()) {
                    WmsOutReceiptTaskMaterial taskMaterial = taskMaterialOptional.get();
                    CrossShearCallMaterialRequestVO vo = JSONObject.parseObject(taskMaterial.getExtraText()
                            , CrossShearCallMaterialRequestVO.class);
                    MDSTransverseShearingIsCalledMaterialRequestVO request = new MDSTransverseShearingIsCalledMaterialRequestVO();
                    request.setSite(vo.getSite());
                    request.setTaskCode(vo.getTaskCode());
                    request.setOperateTime(new Date());
                    request.setTaskType(vo.getTaskType());
                    request.setOperatorName(vo.getOperatorName());
                    request.setLineName(vo.getLineName());
                    request.setOrderCode(vo.getOrderCode());

                    request.setPrepareArea(wmsOutReceiptTask.getMaterialSite());
                    request.setTaskStatus(MdsStatusEnum.TASK_RECEIVED.getCode());
                    if (ObjectUtil.isNotNull(vo.getDetail())) {
                        List details = vo.getDetail().stream().map(o -> (Object) o).collect(Collectors.toList());
                        request.setDetail(details);
                    }
                    MdsBaseResponseVO mdsBaseResponseVO = mdsDockingRepeaterService.mdsTransverseShearingIsCalledMaterial(request);
                    if (ObjectUtils.isNotEmpty(mdsBaseResponseVO)) {
                        if (FlowConstant.SUCCESS.equals(mdsBaseResponseVO.getTarget()) && ObjectUtils.isNotEmpty(mdsBaseResponseVO.getData())) {
                            taskMaterial.setExtraResponse(JSON.toJSONString(mdsBaseResponseVO.getData()));
                            wmsOutReceiptTaskMaterialService.updateById(taskMaterial);
                        }
                        if (FlowConstant.ERROR.equals(mdsBaseResponseVO.getTarget())) {
                            result.set(false);
                        }
                    } else {
                        result.set(false);
                    }
                } else {
                    result.set(false);
                }
            }
            // 可配送
            if (ObjectUtils.compare(type, 1) == 0) {
                if (taskMaterialOptional.isPresent()) {
                    WmsOutReceiptTaskMaterial taskMaterial = taskMaterialOptional.get();
                    CrossShearCallMaterialRequestVO vo = JSONObject.parseObject(taskMaterial.getExtraText()
                            , CrossShearCallMaterialRequestVO.class);
                    MDSDeliveryRequestVO request = new MDSDeliveryRequestVO();
                    request.setSite(vo.getSite());

                    request.setPrepareSite(wmsOutReceiptTask.getMaterialSite());
                    request.setTaskCode(vo.getTaskCode());
                    request.setOperateTime(new Date());
                    request.setTaskType(vo.getTaskType());
                    request.setOperatorName(vo.getOperatorName());
                    request.setLineName(vo.getLineName());
                    request.setTaskStatus(MdsStatusEnum.SEND_MATERIAL_READY.getCode());
                    MdsBaseResponseVO mdsBaseResponseVO = mdsDockingRepeaterService.delivery(request);
                    if (ObjectUtils.isNotEmpty(mdsBaseResponseVO)) {
                        if (FlowConstant.ERROR.equals(mdsBaseResponseVO.getTarget())) {
                            result.set(false);
                        }
                    } else {
                        result.set(false);
                    }
                } else {
                    result.set(false);
                }
            }
        } catch (Exception e) {
            log.error("C13DmsOutbound:异常，{}", e.getMessage(), e);
            result.set(false);
        }
        return result.get();
    }
}
